package org.menacheri.jetserver.dao;

import org.menacheri.jetserver.app.impl.Player;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import com.mongodb.QueryBuilder;

public class UserMongoDAOImpl implements UserMongoDAO {
	@Autowired
	MongoOperations mongoTemplate;

	@Override
	public Player getUser(Player user) {
		Query searchPlayerQuery = new Query();
		if(user.getId() != null) {
			Criteria criteria = new Criteria();
			criteria.where("id").is(user.getId());
			searchPlayerQuery.addCriteria(criteria);
		}
		
		if(user.getUsername() != null) {
			Criteria criteria = new Criteria();
			criteria.where("username").is(user.getUsername());
			searchPlayerQuery.addCriteria(criteria);
		}
		
		if(user.getPassword() != null) {
			Criteria criteria = new Criteria();
			criteria.where("password").is(user.getUsername());
			searchPlayerQuery.addCriteria(criteria);
		}
		
		return mongoTemplate.findOne(searchPlayerQuery, Player.class);
	}
	
}
